<?php

class Person
{
	private $PersonID;
	private $Surname;
	private $FirstName;
	private $Emails     = array();
	private $Groups     = array();
	private $Telephones = array();
	private $Addresses  = array();
	
	private $Database;
	
	public function __construct($PersonID)
	{
		$this->Database=$GLOBALS['DATABASE']->Recopy();
		
		if($PersonID==0)
		{
			$sql = "INSERT INTO MusicAdmin_People (Surname) VALUES ('')";
			$this->Database->executeSQL($sql);
			$PersonID = $this->Database->getInsertID();
		}
		
		$sql="SELECT * FROM MusicAdmin_People WHERE PersonID='".$PersonID."'";
//		echo $sql;
		$this->Database->executeSQL($sql);
		while($row=$this->Database->getRow())
		{
			$this->PersonID = $PersonID;
			if(isset($row['Surname']))    $this->Surname=   $row['Surname'];
			if(isset($row['FirstName']))  $this->FirstName= $row['FirstName'];
		}
		
		$sql="SELECT * FROM MusicAdmin_Emails WHERE PersonID='".$PersonID."'";
		$this->Database->executeSQL($sql);
		while($row=$this->Database->getRow())
		{
			if(isset($row['EmailAddress'])) array_push($this->Emails,$row['EmailAddress']);
		}
		
		$sql="SELECT * FROM MusicAdmin_Groups WHERE PersonID='".$PersonID."'";
		$this->Database->executeSQL($sql);
		while($row=$this->Database->getRow())
		{
			if(isset($row['GroupNameID']))  array_push($this->Groups,$row['GroupNameID']);
		}
		
		$sql="SELECT * FROM MusicAdmin_Telephones WHERE PersonID='".$PersonID."'";
		$this->Database->executeSQL($sql);
		while($row=$this->Database->getRow())
		{
			if(isset($row['Telephone']))  array_push($this->Telephones,$row['Telephone']);
		}
		
		$sql="SELECT * FROM MusicAdmin_Addresses WHERE PersonID='".$PersonID."'";
		$this->Database->executeSQL($sql);
		$address =  array();
		while($row=$this->Database->getRow())
		{
			$addressID = (isset($row['AddressID']))?$row['AddressID']:"";
			$address1 = (isset($row['Address1']))?$row['Address1']:"";
			$address2 = (isset($row['Address2']))?$row['Address2']:"";
			$address3 = (isset($row['Address3']))?$row['Address3']:"";
			$address4 = (isset($row['Address4']))?$row['Address4']:"";
			$postcode = (isset($row['Postcode']))?$row['Postcode']:"";
			$county   = (isset($row['County']))  ?$row['County']:"";
			$country  = (isset($row['Country'])) ?$row['Country']:"";

			$address = new Address
			($addressID,$address1,$address2,$address3,$address4,$postcode,$county,$country);
			
															
			array_push($this->Addresses,$address);
		}
		
				
	}
	
	public function setSurname($surname)
	{
		$this->Surname=$surname;
		$sql =  "UPDATE MusicAdmin_People ";
		$sql .= "SET Surname = '".$surname."' ";
		$sql .= "WHERE PersonID = '".$this->PersonID."'";
//		echo $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function setFirstName($firstname)
	{
		$this->FirstName=$firstname;
		$sql =  "UPDATE MusicAdmin_People ";
		$sql .= "SET FirstName = '".$firstname."' ";
		$sql .= "WHERE PersonID = '".$this->PersonID."'";
//		echo $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function getID() 	    {return $this->PersonID;}
	public function getSurname()    {return $this->Surname;}
	public function getFirstName()  {return $this->FirstName;}
	public function getEmails()     {return $this->Emails;}
	public function getGroups()     {return $this->Groups;}
	public function getTelephones() {return $this->Telephones;}
	public function getAddresses()  {return $this->Addresses;}
	
	public function addEmail($email)
	{
		$sql = "INSERT INTO MusicAdmin_Emails ";
		$sql .= "(PersonID,EmailAddress) VALUES(".$this->PersonID.",'".$email."')";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function removeEmail($email)
	{
		$sql = "DELETE FROM MusicAdmin_Emails WHERE EmailAddress = '".$email."' AND PersonID = '".$this->PersonID."'";
		print $sql;
		$this->Database->executeSQL($sql);
	}
	
		public function addGroup($groupID)
	{
		$sql = "INSERT INTO MusicAdmin_Groups ";
		$sql .= "(PersonID,GroupNameID) VALUES('".$this->PersonID."','".$groupID."')";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function removeGroup($groupID)
	{
		$sql = "DELETE FROM MusicAdmin_Groups WHERE GroupNameID = '".$groupID."' AND PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
		public function addTelephone($Telephone)
	{
		$sql = "INSERT INTO MusicAdmin_Telephones ";
		$sql .= "(PersonID,Telephone) VALUES('".$this->PersonID."','".$Telephone."')";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function removeTelephone($telephone)
	{
		$sql = "DELETE FROM MusicAdmin_Telephones WHERE Telephone = '".$telephone."' AND PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function addAddress($Address)
	{
		$sql = "INSERT INTO MusicAdmin_Addresses ";
		$sql .= "(PersonID, Address1, Address2, Address3, Address4, Postcode, County, Country) ";
		$sql .= "VALUES('";
		$sql .= $this->PersonID."','";
		$sql .= $Address->getAddress1()."','";
		$sql .= $Address->getAddress2()."','";
		$sql .= $Address->getAddress3()."','";
		$sql .= $Address->getAddress4()."','";
		$sql .= $Address->getPostcode()."','";
		$sql .= $Address->getCounty()."','";
		$sql .= $Address->getCountry()."')";
		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function removeAddress($AddressID)
	{
		$sql = "DELETE FROM MusicAdmin_Addresses WHERE AddressID = '".$AddressID."' AND PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
	}
	
	public function delete()
	{
//		echo "DELETE ME";
		//get all emails
		$sql="DELETE FROM MusicAdmin_Emails WHERE PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
		
		//get all groups
		$sql="DELETE FROM MusicAdmin_Groups WHERE PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
				
		//get all telephones
		$sql="DELETE FROM MusicAdmin_Telephones WHERE PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
				
		//need to get all other tables once we have them!
		
		// Delete person itself
		$sql="DELETE FROM MusicAdmin_People WHERE PersonID = '".$this->PersonID."'";
//		print $sql;
		$this->Database->executeSQL($sql);
				
				
		
	}
	
}
